/*
 * @lc app=leetcode.cn id=946 lang=javascript
 *
 * [946] 验证栈序列
 */

// @lc code=start
/**
 * @param {number[]} pushed
 * @param {number[]} popped
 * @return {boolean}
 */
var validateStackSequences = function(pushed, popped) {
  let stack = [];
  let pushI = 0;
  let popI = 0;
  while(pushI < pushed.length){
    stack.push(pushed[pushI]);
    while(stack.length && (stack[stack.length-1] === popped[popI])){
      stack.pop();
      popI++;
    }
    pushI++;
  }
  return stack.length===0;
};
// @lc code=end

